package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.constant.ProjectConstants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysProjectExpert;
import com.ruoyi.system.domain.SysProjectInfo;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysHistoryMapper;
import com.ruoyi.system.domain.SysHistory;

/**
 * 历史记录Service业务层处理
 * 
 * @author riddou
 * @date 2022-04-15
 */
@Service
public class SysHistoryServiceImpl implements ISysHistoryService 
{
    @Autowired
    private SysHistoryMapper sysHistoryMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysProjectInfoService sysProjectInfoService;

    /**
     * 查询历史记录
     * 
     * @param historyId 历史记录主键
     * @return 历史记录
     */
    @Override
    public SysHistory selectSysHistoryByHistoryId(Long historyId)
    {
        return sysHistoryMapper.selectSysHistoryByHistoryId(historyId);
    }

    /**
     * 查询历史记录列表
     * 
     * @param sysHistory 历史记录
     * @return 历史记录
     */
    @Override
    public List<SysHistory> selectSysHistoryList(SysHistory sysHistory)
    {
        return sysHistoryMapper.selectSysHistoryList(sysHistory);
    }

    /**
     * 新增历史记录
     * 
     * @param sysHistory 历史记录
     * @return 结果
     */
    @Override
    public int insertSysHistory(SysHistory sysHistory)
    {
        sysHistory.setCreateTime(DateUtils.getNowDate());
        return sysHistoryMapper.insertSysHistory(sysHistory);
    }

    /**
     * 修改历史记录
     * 
     * @param sysHistory 历史记录
     * @return 结果
     */
    @Override
    public int updateSysHistory(SysHistory sysHistory)
    {
        return sysHistoryMapper.updateSysHistory(sysHistory);
    }

    /**
     * 批量删除历史记录
     * 
     * @param historyIds 需要删除的历史记录主键
     * @return 结果
     */
    @Override
    public int deleteSysHistoryByHistoryIds(Long[] historyIds)
    {
        return sysHistoryMapper.deleteSysHistoryByHistoryIds(historyIds);
    }

    /**
     * 删除历史记录信息
     * 
     * @param historyId 历史记录主键
     * @return 结果
     */
    @Override
    public int deleteSysHistoryByHistoryId(Long historyId)
    {
        return sysHistoryMapper.deleteSysHistoryByHistoryId(historyId);
    }

    @Override
    public SysHistory buildHistory(SysProjectExpert sysProjectExpert, Long type, Long stage) {
        SysHistory sysHistory = new SysHistory();
        Long state = sysProjectExpert.getProjectState();
        //对应his_oper_info
        SysProjectInfo sysProjectInfo = sysProjectInfoService.selectSysProjectInfoByProjectId(sysProjectExpert.getProjectId());
        SysUser sysUser = sysUserService.selectUserByUserName(sysProjectExpert.getCreateBy());

        sysHistory.setOperation(stage);
        sysHistory.setUserId(sysUser.getUserId());
        sysHistory.setRolename(sysUser.getRoles().get(0).getRoleKey());
        sysHistory.setType(type);
        sysHistory.setStage(stage);
        sysHistory.setOperator(sysProjectExpert.getCreateBy());
        sysHistory.setProjectId(sysProjectInfo.getProjectId());
        sysHistory.setReviewId(sysProjectExpert.getReviewId());
        return sysHistory;
    }

    @Override
    public SysHistory buildHistory(SysProjectInfo sysProjectInfo) {
        SysHistory sysHistory = new SysHistory();
        Long state = sysProjectInfo.getState();
        SysUser sysUser = sysUserService.selectUserByUserName(sysProjectInfo.getCreateBy());

        //对应his_oper_info
        sysHistory.setOperation(state < ProjectConstants.STATE_APPROVED ? state : sysProjectInfo.getStage());
        sysHistory.setOperator(sysProjectInfo.getCreateBy());
        sysHistory.setProjectId(sysProjectInfo.getProjectId());
        sysHistory.setRolename(sysUser.getRoles().get(0).getRoleKey());
        sysHistory.setStage(sysProjectInfo.getStage());
        sysHistory.setType(ProjectConstants.HIS_OPER_STATE);
        sysHistory.setUserId(sysUser.getUserId());
        return sysHistory;
    }
}
